//
//  JZSearchItemsView.m
//  TuDianEducation
//
//  Created by lixinkeji on 2020/9/1.
//  Copyright © 2020 zhangbenchao. All rights reserved.
//


#import "JZSearchItemsView.h"
#import "JZSearchCollectionViewCell.h"

@interface JZSearchItemsView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) UIButton *deleteBtn;
@property (nonatomic,strong) UICollectionView *collectionView;

@property (nonatomic,strong) JZSearchCollectionViewCell *lastCell;
@property (nonatomic,assign) NSInteger currentIndex;
@end

@implementation JZSearchItemsView


-(instancetype)initWithTop:(CGFloat)top dataArray:(NSArray *)dataArray isDelete:(BOOL)isDelete
{
     self = [super init];
       if (self) {
           self.frame = CGRectMake(0, top, TSScreenWidth, TSScaleW(100));
          self.backgroundColor = kWhiteColor;
           [self addSubview:self.titleLabel];
           [self addSubview:self.deleteBtn];
           self.deleteBtn.hidden = !isDelete;
           self.dataArray = dataArray.mutableCopy;
           [self addSubview:self.collectionView];
           self.collectionView.height = [self returnCollectionHeight];
           self.height = self.collectionView.bottom;
           self.currentIndex = 0;
           
       }
       return self;
};
-(CGFloat)returnCollectionHeight{
    NSInteger row = 1;
    CGFloat heightRow = TSScaleW(35) + TSScaleW(15);
    CGFloat widthAll = TSScaleW(15);
    for (int i = 0; i < self.dataArray.count; i ++ ) {
        NSString *string = self.dataArray[i];
         CGFloat width = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, TSScaleW(15)) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:TSScaleW(12)]} context:nil].size.width + TSScaleW(38);
        widthAll += TSScaleW(15) + width;
    }
    row = 1 + (widthAll/TSScreenWidth);
    return heightRow *row;
}
-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [TSWLTools allocLabel:@"" font:systemFont(TSScaleW(15)) textColor:kMainTxtColor frame:CGRectMake(TSScaleW(12), TSScaleW(10), TSScreenWidth - TSScaleW(42), TSScaleW(15)) textAlignment:(NSTextAlignmentLeft)];
    }
    return _titleLabel;
}
-(UIButton *)deleteBtn{
    if (!_deleteBtn) {
        _deleteBtn = [TSWLTools allocButtonTitle:@"" font:systemFont(18) textColor:kMainTxtColor image:[UIImage imageNamed:@"guanbi"] frame:CGRectMake(0, 0, TSScaleW(30), TSScaleW(30)) sel:@selector(deleteBtnAction) taget:self];
        _deleteBtn.right = self.width - TSScaleW(15);
        _deleteBtn.centerY = _titleLabel.centerY;
    }
    return _deleteBtn;
}
-(UICollectionView *)collectionView{
      if (!_collectionView) {
          
            CGFloat left = TSScaleW(15);
            CGFloat space = TSScaleW(15);
             UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
             layout.minimumLineSpacing = space;
             layout.minimumInteritemSpacing = space;
             layout.scrollDirection = UICollectionViewScrollDirectionVertical;

          _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, _titleLabel.bottom + TSScaleW(15), TSScreenWidth, TSScaleW(50)) collectionViewLayout:layout];
             _collectionView.delegate = self;
             _collectionView.dataSource = self;
          
             [_collectionView registerClass:[JZSearchCollectionViewCell class] forCellWithReuseIdentifier:@"JZSearchCollectionViewCell"];
             _collectionView.contentInset = UIEdgeInsetsMake(0, left, 0, left);
             _collectionView.scrollEnabled = YES;
             self.collectionView.backgroundColor = kWhiteColor;
      }
    return _collectionView;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
    
    NSString *callBackString = [self.dataArray objectAtIndex:indexPath.row];
   
    !self.selectBlock?:self.selectBlock(callBackString);
    JZSearchCollectionViewCell *lastcell = (JZSearchCollectionViewCell *)[_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_currentIndex inSection:0]];
    lastcell.isSelct = NO;
    JZSearchCollectionViewCell *cell = (JZSearchCollectionViewCell *)[_collectionView cellForItemAtIndexPath:indexPath];
    cell.isSelct = YES;
    _currentIndex = indexPath.row;
    !self.updateBlock?:self.updateBlock(_currentIndex);
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        
    JZSearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JZSearchCollectionViewCell" forIndexPath:indexPath];
    cell.messageLabel.text = self.dataArray[indexPath.row];
    if (indexPath.row == _currentIndex) {
        cell.isSelct = YES;
    }else{
        cell.isSelct = NO;
    }

    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *string = self.dataArray[indexPath.row];
    CGFloat width = [string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, TSScaleW(15)) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:TSScaleW(12)]} context:nil].size.width + TSScaleW(38);
    return CGSizeMake(width, TSScaleW(32));
}

-(void)deleteBtnAction{
   
    !self.deleteBlock?:self.deleteBlock();
}

-(void)setDataArray:(NSMutableArray *)dataArray{
    _dataArray = dataArray;
    NSMutableArray *array = [NSMutableArray array];
    for (NSDictionary *string in _dataArray) {
        if ([string isKindOfClass:[NSDictionary class]]) {
            [array addObject:string[@"title"]];
        }else{
            [array addObject:string];
        }
        
    }
    _dataArray = array;
    if (!_dataArray.count) {
        [self deleteBtnAction];
        return;
    }
    [self.collectionView removeFromSuperview];
    self.collectionView = nil;
    [self addSubview:self.collectionView];
    self.collectionView.height = [self returnCollectionHeight];
    self.height = self.collectionView.bottom;
    !self.callFrameBack?:self.callFrameBack();
}

@end
